package com.southsmart.springboot.email.service.impl;

import com.southsmart.springboot.email.service.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author : CaoLongMin
 * @version 1.0
 * @since 2021/4/26
 */
@Service
public class EmailServiceImpl implements IEmailService {
    @Value("${spring.mail.username}")
    private String sendName;
    @Autowired
    private JavaMailSender mailSender;
    @Override
    public String sendSimpleEmail(String msg, String email) {
        try {
            SimpleMailMessage mail = new SimpleMailMessage();
            mail.setFrom(sendName);
            mail.setCc(sendName);
            mail.setTo(email);
            mail.setSubject("这是一封简单邮件");
            mail.setText(msg);
            mailSender.send(mail);
            return "发送成功";
        } catch (Exception ex) {
            ex.printStackTrace();
            return "发送失败:" + ex.getMessage();
        }
    }

    @Override
    public String sendImgEmail(String msg, String email) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
            messageHelper.setFrom(sendName);
            messageHelper.setCc(sendName);
            messageHelper.setTo(email);
            messageHelper.setSubject("带静态资源图片的HTML邮件");
            String html = "<div><h1><a name=\"hello\"></a><span>Hello</span></h1><blockquote><p><span>this is a html email.</span></p></blockquote><p>&nbsp;</p><p><span>"
                    + msg + "</span></p><img src='cid:myImg' /></div>";
            messageHelper.setText(html, true);
            File file = new File(ResourceUtils.getURL("classpath:").getPath()+"test.png");
            messageHelper.addInline("myImg", file);
            mailSender.send(message);
            return "发送成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "发送失败：" + e.getMessage();
        }
    }

    @Override
    public String sendFileEmail(String msg, String email) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
            messageHelper.setFrom(sendName);
            messageHelper.setCc(sendName);
            messageHelper.setTo(email);
            messageHelper.setSubject("一封包含附件的邮件");
            messageHelper.setText(msg);
            File file = new File(ResourceUtils.getURL("classpath:").getPath()+"test.txt");
            messageHelper.addAttachment(file.getName(), file);
            mailSender.send(message);
            return "发送成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "发送失败：" + e.getMessage();
        }
    }

    @Override
    public String sendHtmlMsg(String msg, String email) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
            messageHelper.setFrom(sendName);
            messageHelper.setCc(sendName);
            messageHelper.setTo(email);
            messageHelper.setSubject("HTML邮件");
            String html = "<div><h1><a name=\"hello\"></a><span>Hello</span></h1><blockquote><p><span>this is a html email.</span></p></blockquote><p>&nbsp;</p><p><span>"
                    + msg + "</span></p></div>";
            messageHelper.setText(html, true);
            mailSender.send(message);
            return "发送成功";
        } catch (MessagingException e) {
            e.printStackTrace();
            return "发送失败：" + e.getMessage();
        }
    }
}
